﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using YAPL.Language.Ast.Utils;

namespace YAPL.Language.Ast {
	/// <summary>
	/// Represents whole program module. Serves as YAPL Ast root.
	/// functions defined in ProgramDefExpressions are considered to be static.
	/// ProgramBody represents program entry point.
	/// </summary>
	public class ProgramDefExpression : Expression {
		private CodeBlockExpression _programBody;
		public ProgramDefExpression(ExpressionType nodeType, Type type, CodeBlockExpression programBody) : base(nodeType, type, true, false) {
			_programBody = programBody;
		}

		public ProgramDefExpression(ExpressionType nodeType, Type type, bool canRead, bool canWrite, CodeBlockExpression programBody) : base(nodeType, type, canRead, canWrite) {
			_programBody = programBody;
		}



		[Descendant(AstVisit.AfterCurrent)]
		public CodeBlockExpression ProgramBody {
			get { return _programBody; }
		}

		public override bool NodeEquals(Expression other) {
			if (other is ProgramDefExpression) {
				return true;
			}
			return false;
		}

		public override bool RewriteChild(Expression child, Expression newExpression) {
			if (ReferenceEquals(_programBody, child)) {
				_programBody = newExpression as CodeBlockExpression;
				return true;
			}

			return false;
		}
	}
}
